module majiang.Views {
    /**
     * MajiangBtn
     */
    export class MajiangBtn extends eui.Button {
        //eui
        public paiIcon:eui.Image;
        public bgIcon:eui.Image;

        //data
        public paiId:number;
        private _paiState:string = "gai";
        private _forArea:string = "";

        constructor() {
            super();

            this.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onTapBtn,this);
            this.addEventListener(egret.Event.ADDED,this.addedToHupaiArea,this);
        }

        private addedToHupaiArea() {
            // console.log("add:",this.paiId,this._paiState,this._forArea);
            if(this._forArea === "hu"){
                this.addHupaiTween();
            }
        }

        /**
         * setMajingId
         * @param id 麻将牌的id
         */
        public setMajingpai(id:number,state:string,area:string) {
            this.paiId = majiang.Datas.Playing.shengyuPai.getPai();

            this.paiIcon.source = this.paiId + "_png";

            this.setForArea(area);

            this._paiState = state;
            this.invalidateState();
        }

        /**
         * setForArea
         * 设置这张牌 走向那个区
         */
        public setForArea(area:string) {
            this._forArea = area;
        }

        /**
         * openGaipai
         * 打开盖牌
         */
        public openGaipai() {
            if(this._paiState !== "gai"){
                return
            }

            this._paiState = "li";
            this.setForArea("xuan");
            this.invalidateState();
        }

        public getCurrentState():string{
            return this._paiState;
        }

        /**
         * 按下麻将
         */
        private onTapBtn(){
            // console.log(this.paiId,this._paiState,this._forArea);

            if(this._paiState === "gai"){
                return
            }

            if(this._forArea === "xuan"){
                this.xuanpaiTap();
            }else if(this._forArea === "hu"){
                this.hupaiTap();
            }
        }

        /**
         * 是选牌区的麻将牌
         */
        private xuanpaiTap(){
            majiang.Controllers.Play.onTapXuanpai(this);
        }


        /**
         * 是胡牌区的麻将牌
         */
        private hupaiTap(){
            majiang.Controllers.Play.onTapHupai(this);
        }

        /**
         * hupai
         * 胡牌时执行
         */
        public hupai() {
            this._paiState = "dao";
            this.invalidateState();
        }

        /**
         * 放入胡牌区时播放动画
         */
        public addHupaiTween(){
            this.touchEnabled = false;

            this.bgIcon.top = -30;
            this.bgIcon.bottom = 30;
            this.paiIcon.bottom = 40;

            egret.Tween.get(this.bgIcon).wait(300).to({top:0,bottom:0},200);
            egret.Tween.get(this.paiIcon).wait(300).to({bottom:10},200).call(()=>{
                this.touchEnabled = true;
            },this);
        }
    }
}